package com.pet.server.ext.business;

import java.util.List;

import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Article;
import com.pet.server.ext.data.Campaign;
import com.pet.server.ext.data.PetAdopt;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.data.RequestInfo;
import com.pet.server.ext.data.UserInfo;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.cache.ConstantUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Business {

	public JSONObject homeDating(List<Campaign> banners,List<Article> articles,List<Article> pet,List<PetAdopt> adoptList,List<RequestInfo> requests,List<ProductInfo> mall,UserInfo user){
	 	JSONObject o = new JSONObject();
	 	
	 	JSONArray bannerJson=new JSONArray();
	 	//焦点图
	 	for(Campaign cam:banners){
	 		JSONObject co = new JSONObject();
	 		co.put("click_type", cam.getClick_type());
	 		co.put("click_url", cam.getClick_url());
	 		co.put("text", cam.getText());
	 		co.put("img_url", cam.getImg_url());
	 		co.put("targt", cam.getTarget());
	 		co.put("param", StringUtil.dealEmpty(cam.getParam(), null));
            bannerJson.add(co);
	 	}
	 	o.put("banners", bannerJson);
	 	
	 	//阅读
	 	JSONArray articleList=new JSONArray();
		for(Article art:articles){
			JSONObject ao=new JSONObject();
			ao.put("article_id", art.article_id);
			ao.put("create_time",JsonUtil.getTime(art.create_time));
			ao.put("title", art.getTitle());
			ao.put("description", art.description);
			ao.put("view_type", art.view_type);
			ao.put("cate", art.cate);
			ao.put("cate_name", ConstantUtil.getInstance().getName(art.cate));
			ao.put("url", BaseAttribute.article_url+art.article_id);
			
			JSONArray images = new JSONArray();
			if(BaseAttribute.view_type.grid.toString().equals(art.view_type)){
				if(!StringUtil.isEmpty(art.image3)){images.add(art.image3);}
				if(!StringUtil.isEmpty(art.image1)){images.add(art.image1);}
				if(!StringUtil.isEmpty(art.image2)){images.add(art.image2);}
			}else if(BaseAttribute.view_type.small.toString().equals(art.view_type)){
				if(!StringUtil.isEmpty(art.image1)){images.add(art.image1);}
			}if(BaseAttribute.view_type.large.toString().equals(art.view_type)){
				if(!StringUtil.isEmpty(art.image1)){images.add(art.image1);}
			}
		    ao.put("images", images);
			articleList.add(ao);
		}
		o.put("articles", articleList);
		
		//宠物秘籍
		JSONArray petArray=new JSONArray();
		for(Article art:pet){
			JSONObject ao=new JSONObject();
			ao.put("article_id", art.article_id);
			ao.put("create_time",JsonUtil.getTime(art.create_time));
			ao.put("title", art.getTitle());
			ao.put("description", art.description);
			ao.put("view_type", art.view_type);
			ao.put("cate", art.cate);
			ao.put("cate_name", ConstantUtil.getInstance().getName(art.cate));
			ao.put("url", BaseAttribute.article_url+art.article_id);
			
			JSONArray images = new JSONArray();
			if(BaseAttribute.view_type.grid.toString().equals(art.view_type)){
				if(!StringUtil.isEmpty(art.image3)){images.add(art.image3);}
				if(!StringUtil.isEmpty(art.image1)){images.add(art.image1);}
				if(!StringUtil.isEmpty(art.image2)){images.add(art.image2);}
			}else if(BaseAttribute.view_type.small.toString().equals(art.view_type)){
				if(!StringUtil.isEmpty(art.image1)){images.add(art.image1);}
			}if(BaseAttribute.view_type.large.toString().equals(art.view_type)){
				if(!StringUtil.isEmpty(art.image1)){images.add(art.image1);}
			}
		    ao.put("images", images);
		    petArray.add(ao);
		}
		o.put("miji", petArray);
		
		//问答
		JSONArray requestJson=new JSONArray();
		
		for(RequestInfo r:requests){
			JSONObject so = new JSONObject();
			JSONObject u = new JSONObject();
			u.put("nick_name", r.nick_name);
		    u.put("ico_path", r.ico_path);
		    u.put("user_id", r.user_id);
		    so.put("id", r.id);
		    so.put("type",r.type);
		    so.put("score", r.score);
		    so.put("user", u);
		    so.put("question", r.question);
		    so.put("cnt", r.cnt);
		    so.put("create_time",JsonUtil.getTime(r.create_time));
		    //so.put("",)
		    if(user.user_id>0){
			    so.put("sessionid",StringUtil.createSessionid(user.user_id, r.id));
		    }
		    requestJson.add(so);
		}
	   o.put("request",requestJson);
		
		//领养
		JSONArray adoptJson=new JSONArray();

	   for(PetAdopt adopt:adoptList){
		   JSONObject ao = new JSONObject();
		   ao.put("adopt_id", adopt.adopt_id);
		   ao.put("image", adopt.ico_path);
		   ao.put("breed", adopt.breed);
		   ao.put("breed_name", ConstantUtil.getInstance().getName(adopt.breed));
		   ao.put("sex", adopt.sex);
		   ao.put("adopt_id", "");
		   ao.put("adopt_id", "");
		   ao.put("adopt_id", "");
		   adoptJson.add(ao);
	   }
	   o.put("adopt", adoptJson);
	   
	   //mall
	   JSONArray mallJson = new JSONArray();
	   for(ProductInfo p:mall){
		   JSONObject po = new JSONObject();
		   po.put("id", p.id);
		   po.put("product_cate", p.product_cate);
		   po.put("product_name", p.product_name);
		   po.put("product_price",p.product_price);
		   po.put("sale_price", p.sale_price);
		   po.put("source", p.source);
		   po.put("ico_path", p.ico_path);
		   po.put("product_description", p.product_description);
		   mallJson.add(po);
	   }
	   o.put("mall",mallJson);
	   
		
		return o;
	}
	
}
